#include "bar.h"
#include "../memory.h"
#include "../render.h"

may_bar_t *bar_create(void *parent, int x, int y, int w, int h, int value)
{
    may_bar_t *bar = (may_bar_t *)memory_alloc(sizeof(may_bar_t));

    bar->obj.widget_type = widget_type_bar;
    bar->obj.x = x;
    bar->obj.y = y;
    bar->obj.h = h;
    bar->obj.w = w;
    bar->value = value;
        if(parent)
    {
    list_add_tail(&bar->obj.node, &((widget_t *)parent)->node);
    }
    return bar;
}

void bar_remove(may_bar_t *bar)
{
    list_del(&bar->obj.node);
    memory_free(bar);
}

void bar_set_value(may_bar_t *bar, int value)
{
    bar->value = value;
}

void bar_render(void *obj)
{
    may_bar_t* bar = (may_bar_t*)bar;
    render_draw_rounded_rect(bar->obj.x, bar->obj.y, bar->obj.w, bar->obj.h, 4);
    render_draw_rect(bar->obj.x + 2, bar->obj.y + 2, bar->value * (bar->obj.w - 4) / 100, bar->obj.h - 4);
}